package sgg.algorithm.datastructure.stack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;

/**
 * @author: Michael
 * @date: 2025/4/28
 * @desc: 中缀表达式转后缀表达式
 */
public class InfixToReversePolishNotation {

    public static void main(String[] args) {
        String expression = "1+((2+3)*4)-5";

        List<String> list = infixToSuffix(expression);
        System.out.println("中缀表达式 : " + list);
        // 1, +, (, (, 2, +, 3, ), *, 4, ), -, 5 = > 1, 2, 3, +, 4, *, +, 5, -
        List<String> list1 = parseSuffixExpressionList(list);
        System.out.println("后缀表达式 : " + list1);
    }

    public static List<String> parseSuffixExpressionList(List<String> ls) {
        Stack<String> stack1 = new Stack<>(); // 符号栈
        List<String> s2 = new ArrayList<>(); // 储存中间结果的

        for (String item : ls){
            if (item.matches("\\d+")){
                // 如果是一个数，加入s2
                s2.add(item);
            } else if (item.equals("(")){
                stack1.push(item);
            } else if (item.equals(")")){
                // 如果是 右括号）， 则依次弹出s1栈顶的运算符，并压入s2，知道遇到左括号为止，此时将这一对括号丢弃
                while (!stack1.peek().equals("(")){
                    s2.add(stack1.pop());
                }
                stack1.pop(); // 弹出左括号
            } else {
                while (stack1.size() != 0 && Operation.getValue(stack1.peek()) >= Operation.getValue(item)){
                    s2.add(stack1.pop());
                }
                stack1.push(item);
            }
        }

        while (!stack1.isEmpty()){
            s2.add(stack1.pop());
        }

        return s2;
    }

    public static List<String> infixToSuffix(String s) {
        List<String> ls = new ArrayList<String>();
        int i = 0;
        String str;
        char c;
        do {
            if ((c = s.charAt(i)) < 48 || (c = s.charAt(i)) > 57) {
                // 如果c是非数字
                ls.add(String.valueOf(c));
                i++;
            } else {
                // 如果是一个数字
                str = "";
                while (i < s.length() && (c = s.charAt(i)) >= 48 && (c = s.charAt(i)) <= 57) {
                    str += c;
                    i++;
                }
                ls.add(str);
            }
        } while (i < s.length());
        return ls;
    }

    public static List<String> getListString(String suffixExpression) {
        String[] split = suffixExpression.split(" ");
        return Arrays.stream(split).toList();
    }

    public static int calculate(List<String> ls) {
        Stack<String> stack = new Stack<>();
        for (String item : ls) {
            if (item.matches("\\d+")){
                // 如果是多位数
                stack.push(item);
            } else {
                int num2 = Integer.parseInt(stack.pop());
                int num1 = Integer.parseInt(stack.pop());
                int res = 0;
                switch (item) {
                    case "+":
                        res = num1 + num2;
                        break;
                    case "-":
                        res = num1 - num2;
                        break;
                    case "*":
                        res = num1 * num2;
                        break;
                    case "/":
                        res = num1 / num2;
                        break;
                    default:
                        throw new RuntimeException("运算符有误");
                }
                stack.push(String.valueOf(res));
            }
        }
        return Integer.parseInt(stack.pop());
    }
}

class Operation {
    private static int ADD = 1;
    private static int SUB = 1;
    private static int MUL = 2;
    private static int DIV = 2;

    public static int getValue(String operation) {
        switch (operation) {
            case "+":
                return ADD;
            case "-":
                return SUB;
            case "*":
                return MUL;
            case "/":
                return DIV;
            default:
                System.out.println("不存在该运算符 : " + operation);
                return -1;
        }
    }
}